package org.yjc.service;

import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.yjc.entity.Student;
import org.yjc.repository.StudentRepository;

@Service
public class StudentService {
	@Autowired
	private StudentRepository studentDAO;
	
	/**
	 * 学生登陆功能
	 * @param userName 用户名
	 * @param passWord 密码
	 * @return
	 */
	public Student login(String userName, String passWord) {
		return studentDAO.findByUserNameAndPassWord(userName,passWord);
	}
	
	/**
	 * 查询所有学生
	 * @return
	 */
	public List<Student> findAll() {
		return studentDAO.findAll();
	}
	
	/***
	 * 通过班级id查询该班级所有学生
	 * @param tbclassid 班级id
	 * @return
	 */
	public List<Student> findByTbClassId(Integer tbclassid) {
		return studentDAO.findByTbClassId(tbclassid);
	}

	public Page<Student> findByTbClassId(Pageable pageable, Integer tbclassid) {
		return studentDAO.findByTbClassId(pageable,tbclassid);
	}

	public Page<Student> findByTbClassId(Specification spec, Pageable pageable, Integer tbclassid) {
		return studentDAO.findByTbClassId(spec,pageable,tbclassid);
	}

	public void deleteById(Integer id) {
		studentDAO.deleteById(id);
	}

	public void save(Student student) {
		studentDAO.save(student);
	}

	public Student findById(Integer xsid) {
		Optional<Student> optional = studentDAO.findById(xsid);
		if(optional.isPresent()) {
			return optional.get();
		}
		return null;
	}
	
}
